#!/bin/bash
#===============================================================================
#
#          FILE:  json.sh
# 
#         USAGE:  ./json.sh 
# 
#   DESCRIPTION:  查找目录下面的mp3，生成json
# 
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR:   (), 
#       COMPANY:  
#       VERSION:  1.0
#       CREATED:  2013/04/01 14时27分59秒 CST
#      REVISION:  ---
#===============================================================================

#if [ $# -ne 2 ]; then
#    echo "查找目录下面的mp3文件，生成json文件"
#    echo "用法: $0 <dir> <jsonfile>"
#    exit 126
#fi

tmpfile=/tmp/list.$$
tmpfile2=/tmp/list2.$$
#dir=$1
#jsonfile=$2
dir=.
jsonfile=musics.json

/usr/bin/find ${dir} -iname "*mp3" >${tmpfile}
/usr/bin/sed 's#^.*/##' ${tmpfile} > ${tmpfile2}
mv ${tmpfile2} ${tmpfile}
/usr/bin/sed 's#\.mp3$##' ${tmpfile} > ${tmpfile2}
mv ${tmpfile2} ${tmpfile}

/usr/bin/awk 'BEGIN{
print "[";
}
{
print "\"" $0 "\",";
}' ${tmpfile}  > ${jsonfile}

/usr/bin/sed -e '$ s/,$/]/' ${jsonfile} > ${tmpfile2}
mv ${tmpfile2} ${jsonfile}
rm -f ${tmpfile} ${tmpfile2}
